package com.gm.springboot.controller;

import com.github.pagehelper.PageInfo;
import com.gm.springboot.bean.Student;
import com.gm.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Administrator
 * @Date 2020/7/2 15:44
 **/
@Controller
public class StudentController {
    @Autowired
    StudentService studentService;
    @GetMapping("/students")
    public String findStudentAll(@RequestParam(defaultValue = "1")Integer page,
                                 @RequestParam(defaultValue = "5")Integer size,
                                 Model model
                                 ){
        List<Student> list=this.studentService.findStudentAll(page,size);
        PageInfo<Student> pageInfo=new PageInfo<>(list);
        model.addAttribute("students",pageInfo);
        return "student-list";

    }
    @PostMapping("/addStudent")
    public String addStudent(Student student){
        this.studentService.addStudent(student);
        return "redirect:/students";

    }
    @GetMapping("/delStudent/{sid}")
    public String delStudent(@PathVariable("sid")Integer sid){
        this.studentService.delStudent(sid);
        return "redirect:/students";
    }
    @GetMapping("/toUpdStudentPage/{sid}")
    public String toUpdPage(@PathVariable("sid")Integer sid,Model model){

        Student student=this.studentService.findStudentById(sid);
        System.out.println(student);
        model.addAttribute("student",student);
//        return "aaaa";
        return "student-upd";
    }
//
    @PutMapping("/updStudent")
    public String updStudent(Student student){
        System.out.println(student);
        this.studentService.updStudent(student);
        return "redirect:/students";
    }



}
